using System;
using System.Collections.Generic;
using System.Text;

namespace NSimpleDB.Service.VistaDb.Resultsets
{
    internal class Resultset
    {
        private string token;
        private IList<string> entries;
        private DateTime createdAt;


        public Resultset(IList<string> entries)
        {
            this.token = Guid.NewGuid().ToString();
            this.createdAt = DateTime.Now;

            this.entries = entries;
        }


        public string Token
        {
            get
            {
                if (this.entries.Count > 0)
                    return this.token;
                else
                    return null;
            }
        }


        public DateTime CreatedAt
        {
            get { return this.createdAt; }
        }


        public int Count
        {
            get { return this.entries.Count; }
        }


        public string[] GetNextPage(int pageSize)
        {
            List<string> pageEntries = new List<string>();
            for (int i = 0; this.entries.Count > 0 && i < pageSize; i++)
            {
                pageEntries.Add(this.entries[0]);
                this.entries.RemoveAt(0);
            }

            if (this.entries.Count == 0) this.createdAt = new DateTime(1900, 1, 1);
                // guarantee the resultset to be removed by the next GC

            return pageEntries.ToArray();
        }

    }
}
